/*****************************************************************
 *Yuga Technology
 *@author Oplink OMS
 *@copyright all rights reserved (c) zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.core.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.sys.dto.MsgDTO;
import com.oplink.webbase.modules.sys.dto.RespDTO;
import com.oplink.webbase.modules.sys.entity.User;
import com.oplink.webbase.modules.sys.service.AccountService;
import com.oplink.webbase.modules.sys.service.ShiroDbRealm.ShiroUser;
import com.oplink.webbase.modules.sys.web.BaseController;

/**
 * User管理的Controller, 使用Restful风格的Urls:
 * 
 * List page : GET /queue/
 * Create pushlet : GET /queue/pushlet
 * Create page : GET /queue/create
 * Create action : POST /queue/create
 * Update page : GET /queue/update/{id}
 * Update action : POST /queue/update
 * Delete action : GET /queue/delete/{id}
 * 告警Controller
 * 
 * @author Oplink OMS
 * @version 2014-08-28
 */
@Controller
@RequestMapping(value = "core/user")
public class UserController extends BaseController {

	@Autowired
	private AccountService accountService;

	@RequestMapping(value = { "list", "" })
	public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {
		Long userId = getCurrentUserId();
		Page<User> pages = accountService.find(new Page<User>(request, response), user);
		model.addAttribute("pages", pages);
		model.addAttribute("userId", userId);
		model.addAttribute("user", user);
		return "modules/core/userList";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public User updateForm(@PathVariable("id") Long id) {
		return accountService.getUser(id);
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
		try {
			accountService.updateUser(user);
			redirectAttributes.addFlashAttribute("message", "更新用户" + user.getLoginName() + "成功");
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}

	@RequiresPermissions("modules/core:user:view")
	@RequestMapping(value = "form")
	public String form(User user, Model model) {
		model.addAttribute("user", user);
		return "modules/core/userForm";
	}

	@RequiresPermissions("modules/core:user:edit")
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("userId", getCurrentUserId());
		model.addAttribute("action", "create");
		return "modules/core/user/create";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(User user, RedirectAttributes redirectAttributes, Model model) {
		try {
			accountService.registerUser(user);
			redirectAttributes.addFlashAttribute("username", user.getLoginName());
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public RespDTO delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		RespDTO retn = RespDTO.createInst();
		try {
			if (!"".equals(id)) {
				String[] delList = id.split(",");
				for (String idItem : delList) {
					accountService.deleteUser(Long.valueOf(idItem));
				}
				redirectAttributes.addFlashAttribute("message", "删除任务成功");
				retn.setResult(RespDTO.SUCCESS, MsgDTO.OK_SAVE_SUCCESS);
			} else {
				retn.setResult(RespDTO.ERROR, MsgDTO.ERR_INPUT_PARAM);
			}
			return retn;
		} catch (Exception e) {
			retn.setResult(RespDTO.ERROR, e.getMessage());
		}
		return retn;
	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
